HDU 4499 Cannon (搜索)

Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 21    Accepted Submission(s): 14


Problem Description
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other chessman along the route or perform an eat action. The eat action, however, is the main concern in this problem. 
An eat action, for example, Cannon A eating chessman B, requires two conditions: 
1、A and B is in either the same row or the same column in the chess grid. 
2、There is exactly one chessman between A and B. 
Here comes the problem. 
Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying that any two cannons are not able to eat each other. It is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell.
 

 

Input
There are multiple test cases. 
In each test case, there are three positive integers N, M and Q (1<= N, M<=5, 0<=Q <= N x M) in the first line, indicating the row number, column number of the grid, and the number of the existing chessmen. 
In the second line, there are Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. It guarantees no pieces share the same cell.
 

 

Output
There is only one line for each test case, containing the maximum number of cannons.
 

 

Sample Input
4 4 2 1 1 1 2 5 5 8 0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0
 

 

Sample Output
8 9
 

 

Source
 

 

Recommend
liuyiding
 

 

 

 

 

数据范围很小,明显是搜索。

主要剪枝,就是不要和前面的冲突了、

 

 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2013/8/24 14:38:00
 4 File Name     :F:\2013ACM练习\比赛练习\2013通化邀请赛\1007.cpp
 5 ************************************************ */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 int n,m;
21 int g[10][10];
22 int ans ;
23 
24 void dfs(int x,int y,int cnt)
25 {
26     if(x >= n)
27     {
28         ans = max(ans,cnt);
29         return;
30     }
31     if(y >= m)
32     {
33         dfs(x+1,0,cnt);
34         return;
35     }
36     if(g[x][y] == 1)
37     {
38         dfs(x,y+1,cnt);
39         return;
40     }
41     dfs(x,y+1,cnt);
42     bool flag = true;
43     int t;
44     for(t = x-1;t >= 0;t--)
45         if(g[t][y])
46         {
47             break;
48         }
49     for(int i = t-1;i >= 0;i--)
50         if(g[i][y])
51         {
52             if(g[i][y]==2)flag = false;
53             break;
54         }
55     if(!flag)return;
56     for(t = y-1;t >= 0;t--)
57         if(g[x][t])
58             break;
59     for(int j = t-1;j >= 0;j--)
60         if(g[x][j])
61         {
62             if(g[x][j] == 2)flag = false;
63             break;
64         }
65     if(!flag)return;
66     g[x][y] = 2;
67     dfs(x,y+1,cnt+1);
68     g[x][y] = 0;
69 }
70 
71 
72 int main()
73 {
74     //freopen("in.txt","r",stdin);
75     //freopen("out.txt","w",stdout);
76     int Q;
77     int u,v;
78     while(scanf("%d%d%d",&n,&m,&Q) == 3)
79     {
80         memset(g,0,sizeof(g));
81         while(Q--)
82         {
83             scanf("%d%d",&u,&v);
84             g[u][v] = 1;
85         }
86         ans = 0;
87         dfs(0,0,0);
88         printf("%d\n",ans);
89     }
90     return 0;
91 }

 

 

 

 

 

 

 

 

posted on 2013-08-24 18:39  kuangbin  阅读(866)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: